package com.eascs.web.demo.db;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;

@Service
public class DbDemoService {

	@Inject
	private DbDemoDao dbDemoDao;

	public void clean() {
		dbDemoDao.clean();
	}

	public void init() {
		dbDemoDao.init();
	}

	@Transactional(transactionManager = "transactionManager")
	public void add(DbDemoEntity dbDemoEntity) {
		dbDemoDao.add(dbDemoEntity);
		if ("123".equals(dbDemoEntity.getName())) {
			throw new IllegalArgumentException();
		}
		DbDemoEntity dbDemoEntity2 = new DbDemoEntity();
		dbDemoEntity2.setAddress("address");
		dbDemoEntity.setAddress("test11");
		dbDemoDao.add(dbDemoEntity2);
	}

	@Transactional(transactionManager = "transactionManager")
	public void testTransactional() {
		DbDemoEntity dbDemoEntity2 = new DbDemoEntity();
		dbDemoEntity2.setAddress("address");
		dbDemoDao.add(dbDemoEntity2);
		if (System.currentTimeMillis() > 0)
			throw new RuntimeException("testTransactional");
	}

	@Transactional(transactionManager = "transactionManager")
	public void testTransactional(boolean success) {
		DbDemoEntity dbDemoEntity2 = new DbDemoEntity();
		dbDemoEntity2.setAddress("address");
		dbDemoDao.add(dbDemoEntity2);
		if (!success)
			throw new RuntimeException("testTransactional");
	}

	public List<DbDemoEntity> list() {
		return dbDemoDao.list();
	}

	public PageResult<DbDemoEntity> findByPage(SqlPageQuery<DbDemoEntity> sqlPageQuery) {
		return dbDemoDao.findByPage(sqlPageQuery);
	}

}
